local dap = require("dap")

-- require("dap.ext.vscode").json_decode = require("json5").parse
require("dap.ext.vscode").load_launchjs(nil, { cppdbg = { "c", "cpp" } })

-- NOTE: pip3 install debugpy
dap.adapters.python = {
	type = "executable",
	-- NOTE: 这里的 command 只能是个 string 而不能是 function
	-- command = 'path/to/virtualenvs/debugpy/bin/python';
	-- command = function ()
	--   if vim.fn.executable("/opt/homebrew/bin/python3") == 1 then
	--     return "/opt/homebrew/bin/python3"
	--   elseif vim.fn.executable("/usr/bin/python3") == 1 then
	--     return "/usr/bin/python3"
	--   end
	-- end;
	command = "/opt/homebrew/bin/python3",

	args = { "-m", "debugpy.adapter" },
}

dap.configurations.python = {
	{
		-- The first three options are required by nvim-dap
		type = "python", -- the type here established the link to the adapter definition: `dap.adapters.python`
		request = "launch",
		name = "Launch file",

		-- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options

		program = "${file}", -- This configuration will launch the current file if used.
		pythonPath = function()
			-- debugpy supports launching an application with a different interpreter then the one used to launch debugpy itself.
			-- The code below looks for a `venv` or `.venv` folder in the current directly and uses the python within.
			-- You could adapt this - to for example use the `VIRTUAL_ENV` environment variable.
			local cwd = vim.fn.getcwd()
			if vim.fn.executable(cwd .. "/venv/bin/python") == 1 then
				return cwd .. "/venv/bin/python"
			elseif vim.fn.executable(cwd .. "/.venv/bin/python") == 1 then
				return cwd .. "/.venv/bin/python"
			elseif vim.fn.executable("/opt/homebrew/bin/python3") then
				return "/opt/homebrew/bin/python3"
			else
				return "/usr/bin/python3"
			end
		end,
	},
}

-- NOTE: https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#ccrust-via-lldb-vscode
-- NOTE: brew install llvm
dap.adapters.lldb = {
	type = "executable",
	-- command = '/usr/bin/lldb-vscode', -- adjust as needed, must be absolute path
	command = "/opt/homebrew/opt/llvm/bin/lldb-vscode",
	name = "lldb",
}
dap.configurations.cpp = {
	{
		name = "Launch",
		type = "lldb",
		request = "launch",
		program = function()
			return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file")
		end,
		cwd = "${workspaceFolder}",
		stopOnEntry = false,
		args = {},

		-- 💀
		-- if you change `runInTerminal` to true, you might need to change the yama/ptrace_scope setting:
		--
		--    echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
		--
		-- Otherwise you might get the following error:
		--
		--    Error on launch: Failed to attach to the target process
		--
		-- But you should be aware of the implications:
		-- https://www.kernel.org/doc/html/latest/admin-guide/LSM/Yama.html
		-- runInTerminal = false,
	},
}

-- If you want to use this for Rust and C, add something like this:
dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp
